import trimesh
try:
    import DetailDesignV2.jxq.group as group
except:
    import group as group

# 检测气缸与工件是否存在碰撞
def detect_collision(obj=None, env=None,dectect_list=['气缸','支柱']):
    collision_rigion = trimesh.collision.CollisionManager()
    jig=None
    for k,v in obj.elements.items():
        for test_str in dectect_list:
            if test_str in k:
                m = v['instance'].get_mesh()
                m.apply_transform(v['transform'])
                if jig is None:
                    jig=m
                else:
                    jig+=m
    jig.apply_transform(obj.transform)
    collision_rigion.add_object(name='tt',mesh=jig)
    if env:
            collision_rigion.add_object(name="工件", mesh=env)
            result = collision_rigion.in_collision_internal()
            print("Air和工件存在碰撞：", result)
            return result

def detect_distance_between_mesh(obj,env,dectect_list=['气缸','支柱']):
    collision_rigion = trimesh.collision.CollisionManager()
    jig=None
    for k,v in obj.elements.items():
        for test_str in dectect_list:
            if test_str in k:
                m = v['instance'].get_mesh()
                m.apply_transform(v['transform'])
                if jig is None:
                    jig=m
                else:
                    jig+=m
    jig.apply_transform(obj.transform)
    collision_rigion.add_object(name='tt', mesh=jig)
    distance=collision_rigion.min_distance_single(env)
    print(distance)
    return distance

def detect_distance_between_mesh_and_setting_box(obj_settting_box,env):
    collision_rigion = trimesh.collision.CollisionManager()
    collision_rigion.add_object(name='tt', mesh=obj_settting_box)
    distance=collision_rigion.min_distance_single(env)
    print(distance)
    return distance